package com.deercrowing.takeaway.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deercrowing.takeaway.context.BaseContext;
import com.deercrowing.takeaway.dao.DishesMapper;
import com.deercrowing.takeaway.dao.SetmealsMapper;
import com.deercrowing.takeaway.dao.ShoppingCartMapper;
import com.deercrowing.takeaway.entity.DishesEntity;
import com.deercrowing.takeaway.entity.SetmealsEntity;
import com.deercrowing.takeaway.entity.ShoppingCartEntity;
import com.deercrowing.takeaway.service.ShoppingCartService;
import com.deercrowing.takeaway.vo.ShoppingCartVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车(ShoppingCart)表服务实现类
 *
 * @author guoyongxiang
 * @since 2024-07-22 14:11:57
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCartEntity> implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishesMapper dishesMapper;

    @Autowired
    private SetmealsMapper setmealsMapper;

    @Override
    public void insertByShoppingCart(ShoppingCartVO shoppingCartVO) {
        //判断当前加入到购物车中的商品是否已经存在了
        ShoppingCartEntity shoppingCartEntity = new ShoppingCartEntity();
        BeanUtils.copyProperties(shoppingCartVO,shoppingCartEntity);
        Long userId = BaseContext.getCurrentId();
        shoppingCartEntity.setUserId(userId);

        List<ShoppingCartEntity> list = shoppingCartMapper.selectByCondition(shoppingCartEntity);

        //如果已经存在了，只需要将数量加一
        if(list != null && list.size() > 0){
            ShoppingCartEntity cart = list.get(0);
            Integer id = Short.toUnsignedInt(cart.getNumber()) + 1;
            cart.setNumber(id.shortValue());//update shopping_cart set number = ? where id = ?
            shoppingCartMapper.updateNumberById(cart);
        }else {
            //如果不存在，需要插入一条购物车数据
            //判断本次添加到购物车的是菜品还是套餐
            Integer dishId = shoppingCartVO.getDishId();
            if(dishId != null){
                //本次添加到购物车的是菜品
                DishesEntity dishesEntity = dishesMapper.selectById(dishId);
                shoppingCartEntity.setName(dishesEntity.getName());
                shoppingCartEntity.setImage(dishesEntity.getImage());
                shoppingCartEntity.setAmount(dishesEntity.getPrice().doubleValue());
            }else{
                //本次添加到购物车的是套餐
                Integer setmealId = shoppingCartVO.getSetmealId();
                SetmealsEntity setmealsEntity = setmealsMapper.selectById(setmealId);
                shoppingCartEntity.setName(setmealsEntity.getName());
                shoppingCartEntity.setImage(setmealsEntity.getImage());
                shoppingCartEntity.setAmount(setmealsEntity.getPrice());
            }
            shoppingCartEntity.setNumber((short)1);
            shoppingCartEntity.setCreateTime(LocalDateTime.now());
            shoppingCartEntity.insert();
        }
    }

    @Override
    public List<ShoppingCartEntity> selectListByShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCartEntity shoppingCartEntity = new ShoppingCartEntity();
        shoppingCartEntity.setUserId(userId);
        List<ShoppingCartEntity> list = shoppingCartMapper.selectByCondition(shoppingCartEntity);
        return list;
    }

    @Override
    public void deleteByShoppingCart(ShoppingCartVO shoppingCartVO) {
        ShoppingCartEntity shoppingCartEntity = new ShoppingCartEntity();
        BeanUtils.copyProperties(shoppingCartVO,shoppingCartEntity);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCartEntity.setUserId(BaseContext.getCurrentId());

        List<ShoppingCartEntity> list = shoppingCartMapper.selectByCondition(shoppingCartEntity);

        if(list != null && list.size() > 0){
            shoppingCartEntity = list.get(0);

            Integer number = shoppingCartEntity.getNumber().intValue();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(shoppingCartEntity.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                Integer number_id = shoppingCartEntity.getNumber() - 1;
                shoppingCartEntity.setNumber(number_id.shortValue());
                shoppingCartMapper.updateNumberById(shoppingCartEntity);
            }
        }
    }

    @Override
    public void cleanShoppingCart() {
        //获取到当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }
}

